﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using DbSharp.Framework.Driver;

namespace DbSharp.Framework.Storage
{
    static class StorageTextHelper
    {
        /// <summary>
        /// Replace null with string.empty
        /// </summary>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        private static object ReplaceNullWithEmptyString(object fieldValue)
        {           
            if ((fieldValue == null) || (fieldValue is DBNull))
                return string.Empty;
            else
                return fieldValue;
        }


 

        //public static bool PossibleNeedUseCData(object fieldValue)
        //{
        //    if (fieldValue.GetType() == typeof(string))
        //    {
        //        if (string.IsNullOrEmpty((string)fieldValue))
        //            return false;
        //        else
        //            return true;

        //    }
        //    return false;
        //}

        public static bool PossibleNeedUseCData(string fieldValueText, int valueType, ITestDataSource dbPlugin)
        {
            if (string.IsNullOrEmpty(fieldValueText))
                return false;
            else if (dbPlugin.FieldPersistenceService.IsStringType(valueType))
                return true;
            else
                return true;
        }

        
        
        /// <summary>
        /// In order to output text diff, one record should be put in single line. it is necessary to escape the CRLF 
        /// Escape CRLF with string "*CR**LF*"
        /// </summary>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static string EscapeNewlineMark(string fieldValue)
        {
            return fieldValue;
            //
            //TODO: in order to increase performance, do not use the method
            //
            #region Slow speed algorithm

            string fieldValueText = (string)fieldValue;
            if (String.IsNullOrEmpty(fieldValueText))
                return string.Empty;
            else
            {
                string text = fieldValueText.Replace("\n", "*CR*");
                return text.Replace("\r", "*LF*");
            }
            #endregion
        }
    }
}
